﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hotel
{
    public partial class Service : Form
    {
        public Service()
        {
            InitializeComponent();
        }
        CNDatabase dt = new CNDatabase();
        

        private void TbServiceName_TextChanged(object sender, EventArgs e)
        {
            TbIDService.Text = dt.getOneData("select max(IDService)+1 from Service").ToString();
        }

        private void TbPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Enter only numeric values in this field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                e.Handled = true;
            }
        }
        BindingSource bdS = new BindingSource();

        private void loadData()
        {
            bdS.DataSource = dt.getDataset("select * from Service").Tables[0];
            grvService.DataSource = bdS;
            bindingNavigator1.BindingSource = bdS;
            this.TbDes.DataBindings.Add("Text", bdS, "Description");
            this.TbServiceName.DataBindings.Add("Text", bdS, "NameService");
            this.TbPrice.DataBindings.Add("Text", bdS, "Price");
            this.TbUnit.DataBindings.Add("Text", bdS, "Unit");
            this.TbIDService.DataBindings.Add("Text", bdS, "IDService");
        }

        private void Service_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string typeservice = "";
                if (rb1.Checked)
                    typeservice = "Foods";
                if (rb2.Checked)
                    typeservice = "Dringking";
                if (rb3.Checked)
                    typeservice = "Entertaitment";
                if (rb4.Checked)
                    typeservice = txtBonus.Text;
                //typeservice = "Bonus";
                string qr = "INSERT INTO Service(IDService,TypeService, NameService,Description,Price, Unit) VALUES('"
                    + TbIDService.Text + "', '" + typeservice + "', '" + TbServiceName.Text + "', '" + TbDes.Text + "', '"
                    + TbPrice.Text + "', '" + TbUnit.Text + "' )";
                dt.xlExecuteNonQuery(qr);
                loadData();
               
            }
            catch (Exception)
            {
                dt.closeConnect();
                MessageBox.Show("Added new service's information successfully !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               // MessageBox.Show("Error");
            }
        }
        

      
    }
}
